<template>
	<view>
		<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
		  <swiper-item v-for="(item, i) in goods_info.pics" :key="i">
		    <image class="image" :src="item.pics_big" @click="preview(i)"></image>
		  </swiper-item>
		</swiper>
		<view class="goods_info">
			<view class="price">
				￥{{goods_info.goods_price}}.00
			</view>
			<view class="info">
				<view style="flex: 1;">
					{{goods_info.goods_name}}
				</view>
				<view style="width:100rpx;border-left:1px solid #dedede;margin-left:50rpx;text-align:center;">收藏</view>
			</view>
			<view class="bz">快递：免运费</view>
		</view>
		<view>
			<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		</view>
		<view style="height:100rpx;"></view>
		<view class="navButton">
			<uni-goods-nav :fill="true" @click="onClick" @buttonClick="buttonClick"/>
		</view>
	</view>
</template>

<script>
	import {myStore} from '../../store/store.js';
	const catStore = myStore();
	export default {
		data() {
			return {
				id:'',
				goods_info:{}
			};
		},
		onLoad(options){
			// console.log(options.goods_id)
			this.id = options.goods_id;
			this.loadGoodsDetails()
		},
		methods:{
			loadGoodsDetails(){
				uni.$http.get('/api/public/v1/goods/detail',{goods_id:this.id}).then((res)=>{
					this.goods_info = res.data.message;
					//解决图片底部 空白间隙 的问题和解决 .webp 格式图片在 ios 设备上无法正常显示的问题
					this.goods_info.goods_introduce = this.goods_info.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg');
					console.log(this.goods_info)
				})
			},
			preview(i){
				// 调用 uni.previewImage() 方法预览图片
				uni.previewImage({
					// 预览时，默认显示图片的索引
					current: i,
					// 所有图片 url 地址的数组
					urls: this.goods_info.pics.map(x => x.pics_big)
				})
			},
			onClick(e){
				console.log(e);
			},
			buttonClick(e){
				console.log(e);
				if(e.index == 0){
					uni.showToast({
						title:"添加成功"
					})
					
					let goods = {
						goods_id:this.goods_info.goods_id, 
						goods_name:this.goods_info.goods_name, 
						goods_price:this.goods_info.goods_price, 
						goods_count:1, 
						goods_small_logo:this.goods_info.goods_small_logo, 
						goods_state:true,
					}
					//将商品添加到pinia的cart数组中
					catStore.addCart(goods);
					
					//延迟跳转
					setTimeout(()=>{
						uni.switchTab({
							url:"/pages/cart/cart"
						})
					},1000)
				}
			}
		}
	}
</script>

<style lang="scss">
	.swiper {
	  height: 750rpx;
	  .image {
	    width: 100%;
	    height: 100%;
	  }
	}
	.goods_info{
		padding:20rpx;
	}
	.price{
		font-size:32px;
		font-weight:bold;
		color:red;
	}
	.info{
		display:flex;
		justify-content:space-between;
		
	}
	.example-body {
		padding: 0;
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
	}
	.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
	.navButton{
		position:fixed;
		bottom:0;
		width:100%;
	}
</style>
